extends ./layout.pug

block content
  - const d = pageInfo.data
  - const daTitle = daurlAlias ? daurlAlias : daurl
  - const downloadName = (daTitle + '-' + options.browser + '-' + timestamp).replace(/(https?\:\/\/)|( )|(:)/ig, '')
  h1 Page summary

  h5.url
    a(href=daurl) #{decodeURIComponent(daTitle)}

  include ../includes/pageRunInfo

  if d.browsertime && d.browsertime.pageSummary
    b #{d.browsertime.pageSummary.info.title}
    p !{d.browsertime.pageSummary.info.description}

  p
    .large All runs:&nbsp;
      each val, index in runPages
        - value = Number(index) + 1
        a(href='./' + value + '.html') #{value}
        if (value !== Object.keys(runPages).length)
          | &nbsp;-
          |

  if pageInfo.errors
    .errors
      b Errors
      ul
        each errors, tool in pageInfo.errors
          each error in errors
            li #{error} 
              b (#{tool})
  include ./tabs.pug

  #tabSections
    section#summary-panel
      h2 Summary
      .row
        .one-half.column
          table
            tr
              th Metric
              th Value
            - performanceScore = get(d, 'coach.pageSummary.advice.performance.score')
            if performanceScore
              tr
                td Performance Score
                td #{performanceScore}
            - totalTransferSize = get(d, 'pagexray.pageSummary.transferSize')
            if totalTransferSize
              tr
                td Total Page Transfer Size
                td #{h.size.format(totalTransferSize)}
            - requests = get(d, 'pagexray.pageSummary.requests')
            if requests
              tr
                td Requests
                td #{requests}
            - ttfb = get(d, 'browsertime.pageSummary.statistics.timings.pageTimings.backEndTime.median') 
            if ttfb
              tr
                td TTFB [median]
                td #{h.time.ms(ttfb)}
            - fullyLoaded = get(d, 'browsertime.pageSummary.statistics.timings.fullyLoaded.median')
            if fullyLoaded  
              tr
                td Fully Loaded [median]
                td #{h.time.ms(fullyLoaded)}
            - firstVisualChange = get(d, 'browsertime.pageSummary.statistics.visualMetrics.FirstVisualChange.median')
            - firstPaint = get(d, 'browsertime.pageSummary.statistics.timings.firstPaint.median')
            if firstVisualChange
              tr
                td First Visual Change [median]
                td #{h.time.ms(firstVisualChange)}
            else if firstPaint
              tr
                td First Paint [median]
                td #{h.time.ms(firstPaint)}  
            - fcp = get(d, 'browsertime.pageSummary.statistics.timings.paintTiming["first-contentful-paint"].median') 
            if fcp
              tr
                td FCP [median]
                td #{h.time.ms(fcp)}         
            - lcp = get(d, 'browsertime.pageSummary.statistics.timings.largestContentfulPaint.renderTime.median')
            if lcp
              tr
                td LCP [median]
                td #{h.time.ms(lcp)}
            - cls = get(d, 'browsertime.pageSummary.statistics.pageinfo.layoutShift.median')
            if cls || cls === 0
                tr
                  td CLS [median]
                  td #{cls}
            - tbt = get(d, 'browsertime.pageSummary.statistics.cpu.longTasks.totalBlockingTime.median')
            if tbt  
              tr
                td Total Blocking Time [median]
                td #{h.time.ms(tbt)}
            - mpFID = get(d, 'browsertime.pageSummary.statistics.cpu.longTasks.maxPotentialFid.median')
            if mpFID
              tr
                td Max Potential FID [median]
                td #{h.time.ms(mpFID)}
            - longTasks = get(d, 'browsertime.pageSummary.statistics.cpu.longTasks.tasks.median')
            if longTasks
              tr
                td CPU long tasks [median]
                td #{longTasks}
            - maxLongTask= get(d, 'browsertime.pageSummary.statistics.cpu.longTasks.durations.max') 
            if maxLongTask 
              tr
                td CPU longest task duration
                td #{h.time.ms(maxLongTask)}
            - speedIndex = get(d, 'browsertime.pageSummary.statistics.visualMetrics.SpeedIndex.median')
            if speedIndex
              tr
                td Speed Index [median]
                td #{h.time.ms(speedIndex)}
            - visualComplete85 = get(d, 'browsertime.pageSummary.statistics.visualMetrics.VisualComplete85.median')
            if visualComplete85
              tr
                td Visual Complete 85% [median]
                td #{h.time.ms(visualComplete85)}
            - visualComplete99 = get(d, 'browsertime.pageSummary.statistics.visualMetrics.VisualComplete99.median')
            if visualComplete99
              tr
                td Visual Complete 99% [median]
                td #{h.time.ms(visualComplete99)}
            - lastVisualChange = get(d, 'browsertime.pageSummary.statistics.visualMetrics.LastVisualChange.median')
            if lastVisualChange
              tr
                td Last Visual Change [median]
                td #{h.time.ms(lastVisualChange)}
            if !d.browsertime && d.webpagetest
              tr
                td Render (first view)
                td #{d.webpagetest.pageSummary.data.median.firstView.render}
              tr
                td Speed Index (first view)
                td #{d.webpagetest.pageSummary.data.median.firstView.SpeedIndex}
              tr
                td Visual Complete 85% (first view)
                td #{d.webpagetest.pageSummary.data.median.firstView.visualComplete85}
              tr
                td Last Visual Change (first view)
                td #{d.webpagetest.pageSummary.data.median.firstView.lastVisualChange}
              tr
                td Requests
                td #{d.webpagetest.pageSummary.data.median.firstView.requestsFull}
            if d.crux && d.crux.pageSummary && d.crux.pageSummary.loadingExperience && d.crux.pageSummary.loadingExperience.ALL && !d.browsertime
              - const cruxMetrics = {first_contentful_paint:'First Contentful Paint (FCP)', largest_contentful_paint: 'Largest Contentful Paint (LCP)', first_input_delay:'First Input Delay (FID)', cumulative_layout_shift: 'Cumulative Layout Shift'}; 
              each name, key in cruxMetrics
                tr 
                  td Fast #{name} (CrUx overall)
                  td #{Number(d.crux.pageSummary.loadingExperience.ALL.data.record.metrics[key].histogram[0].density * 100).toFixed(2)} %
                tr 
                  td Moderate #{name} (CrUx overall)
                  td #{Number(d.crux.pageSummary.loadingExperience.ALL.data.record.metrics[key].histogram[1].density * 100).toFixed(2)} %
                tr 
                  td Slow #{name} (CrUx overall)
                  td #{Number(d.crux.pageSummary.loadingExperience.ALL.data.record.metrics[key].histogram[2].density * 100).toFixed(2)} %
        .one-half.column
            if hasScreenShots
              - const width = options.mobile ? 150 : '100%';
              - const screenshotNo = medianRun ? medianRun.runIndex : 1
              - const screenshotName = 'data/screenshots/' + screenshotNo + '/afterPageCompleteCheck.' + screenShotType
              a(href=screenshotName)
                img.screenshot(src=screenshotName, width=width, alt='Screenshot of run ' + screenshotNo)
            else if !d.browsertime && d.webpagetest
              a(href='data/screenshots/wpt-1-firstView.png')
                img.screenshot(src='data/screenshots/wpt-1-firstView.png', alt='Screenshot of run 1')
      include ./summaryBox.pug

      .downloads
        include ./downloads.pug
        include ./tcpdump.pug

    if d.browsertime && d.browsertime.har
      section#waterfall-panel
        include ../waterfall/index.pug

    if options.browsertime.video
      section#video-panel
        include ../video/index.pug

    if (options.browsertime.video || options.browsertime.visualMetrics) && options.videoParams.createFilmstrip || options.browsertime.chrome && options.browsertime.chrome.enableTraceScreenshots
      section#filmstrip-panel
        include ../filmstrip/index.pug    

    if d.coach && d.coach.pageSummary
      section#coach-panel
        include ../coach/index.pug

    if d.browsertime && d.browsertime.pageSummary
      section#metrics-panel
        include ../metrics/index.pug

    if d.pagexray && d.pagexray.pageSummary
      section#pagexray-panel
        include ../pagexray/index.pug

    if options.cpu || options.browsertime && options.browsertime.chrome && options.browsertime.chrome.collectLongTasks || (options.browsertime && options.browsertime.firefox && options.browsertime.firefox.geckoProfiler)
      section#cpu-panel
        include ../cpu/index.pug  

    if d.thirdparty && d.thirdparty.pageSummary
      section#thirdparty-panel
        include ../thirdparty/index.pug

    each pageSummary in pageSummaries
      - const panelName = pageSummary.id + '-panel'
      section(id=panelName)
        p !{pugs[pageSummary.id]}
